package com.wxsh.test;

import java.util.LinkedHashMap;
import java.util.Map;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.nlp.v20190408.NlpClient;
import com.tencentcloudapi.nlp.v20190408.models.LexicalAnalysisRequest;
import com.tencentcloudapi.nlp.v20190408.models.LexicalAnalysisResponse;
import com.tencentcloudapi.nlp.v20190408.models.PosToken;
import com.wxsh.Constants;

public class TencentTokenize {
	
	public static void main(String[] args) throws TencentCloudSDKException {
		Credential cred = new Credential("AKIDQdZEdlcBUWHlzwc18zpPikVzC7mAH6ev", "1kDOJyLSJtBDyRdZDagU2JK3WhbEafT0");
		NlpClient client = new NlpClient(cred, "ap-guangzhou");
		
		LexicalAnalysisRequest req = new LexicalAnalysisRequest();
		req.setText("欢迎使用腾讯知文自然语言处理。");
		req.setFlag(1L);
		
		LexicalAnalysisResponse resp = client.LexicalAnalysis(req);
		
		LinkedHashMap<Long, Long> numMap = new LinkedHashMap<Long, Long>();
		LinkedHashMap<Long, String> strMap = new LinkedHashMap<Long, String>();
		PosToken[] tokens = resp.getPosTokens();
		for (PosToken token : tokens) {
			String word = token.getWord();
			Long bo = token.getBeginOffset();
			Long lg = token.getLength();
			Long no = numMap.get(bo);
			if(no==null){
				numMap.put(bo, lg);
				strMap.put(bo, word);
			}else{
				if(lg.compareTo(no)<0){
					numMap.put(bo, lg);
					strMap.put(bo, word);
				}
			}
		}
		
		StringBuilder sb = new StringBuilder("");
		for (Map.Entry<Long, String> entry : strMap.entrySet()) {
			String val = entry.getValue();
			sb.append(val);
			sb.append(Constants.SEPARATOR);
		}
		System.out.println(sb.toString());
	}
}

